package org.example.controller.shanjia;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.Product;
import org.example.entity.Shanpin;
import org.example.result.PageResult;
import org.example.result.Pages;
import org.example.result.Result;
import org.example.service.ShanpinService;
import org.example.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/shanjia/shanpin")
@Slf4j
@Api(tags = "商品相关接口")
public class GlshanpinController {

    @Autowired
    ShanpinService shanpinService;
    /**
     * 商品分页查询
     * @param pages
     * @return
     */
    @PostMapping("/cx")
    @ApiOperation(value = "商家分页查询商品")
    public Result<PageResult> shanpinfeye(@RequestBody Pages pages) {
        log.info("分页查询，参数为：{}", pages);
        PageResult pageResult = shanpinService.pageQuery(pages);
        return Result.success(pageResult);
    }


    /**
     * 商品新增
     * @param produc
     * @param images
     * @return
     */
    @PostMapping("/xz")
    @ApiOperation(value = "商家商品新增")
    public Result shanpinxz( @RequestPart("product") Product produc,
                             @RequestPart("images") MultipartFile[] images,
                             @RequestPart("background") MultipartFile background) {

        shanpinService.xz(produc,images,0,background);
        return Result.success();
    }

    /**
     * 商品分类查询
     * @param
     * @return
     */
    @GetMapping ("/xgcx")
    @ApiOperation(value = "商品修改")
    public Result<ProductVO> feleplnamecx(@RequestParam( "id") int id) {
        log.info("商品修改：{}", id);
        return Result.success( shanpinService.xgcx(id));
    }


    /**
     * 商品新增
     * @param
     * @param
     * @return
     */
    @PostMapping("/xgxz")
    @ApiOperation(value = "商家商品修改新增")
    public Result shanpinxgxz( @RequestPart("product") Product product,
                               @RequestPart("images") MultipartFile[] images,
                               @RequestPart("background") MultipartFile background) {
        int id=product.getId();
        log.info("修改新增：{}",product );
        shanpinService.xz(product,images,id, background);
        return Result.success();
    }


    /**
     * 修改商品删除
     * @param
     * @return
     */
    @DeleteMapping ("/xgsc")
    @ApiOperation(value = "修改删除商品")
    public Result shanpinxgsc(@RequestParam( "id") int id) {
        log.info("修改删除：{}", id);
        shanpinService.xgsc(id,0);
        return Result.success();
    }


    /**
     * 修改商品删除image
     * @param
     * @return
     */
    @DeleteMapping ("/xgscimage")
    @ApiOperation(value = "修改删除商品image")
    public Result shanpinxgscimage(@RequestParam( "url") String url) {
        log.info("修改删除image：{}", url);
        shanpinService.xgscimage(url);
        return Result.success();
    }


    /**
     * 状态修改
     * @param product
     * @return
     */
    @PutMapping("/shanpinzt")
    @ApiOperation(value = "修改商品状态")
    public Result shanpinxgzt(@RequestBody Product product) {
        log.info("修改商品状态:{}", product);
        shanpinService.xgzt(product);
        return Result.success();
    }
    /**
     * 商品单删除
     * @param
     * @return
     */
    @DeleteMapping ("/shanpinsc")
    @ApiOperation(value = "单删除商品")
    public Result shanpindansc(@RequestParam( "id") int id) {
        log.info("单删除：{}", id);
        shanpinService.xgsc(id,1);
        return Result.success();
    }

    /**
     * 商品批量删除
     * @param
     * @return
     */
    @DeleteMapping ("/shanpinplsc")
    @ApiOperation(value = "批量删除商品")
    public Result shanpinplsc(@RequestParam( "name") String name,
                              @RequestParam( "status") String status,
                              @RequestParam( "felename") String felename,
                              @RequestParam( "username") String username) {
        log.info("批量删除：{}", name);
        shanpinService.xgplsc(name,status,felename,username);
        return Result.success();
    }

    /**
     * 主页商品查询
     * @param
     * @return
     */
    @GetMapping ("/shanpincx")
    @ApiOperation(value = "主页商品查询")
    public Result<List<Shanpin>> zhuyeshanpincx() {
        log.info("商品查询：{}");
        return Result.success( shanpinService.zhuyeshanpincx());
    }
}
